// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
//
// ███╗   ██╗ ██████╗ ████████╗██╗ ██████╗███████╗
// ████╗  ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝
// ██╔██╗ ██║██║   ██║   ██║   ██║██║     █████╗
// ██║╚██╗██║██║   ██║   ██║   ██║██║     ██╔══╝
// ██║ ╚████║╚██████╔╝   ██║   ██║╚██████╗███████╗
// ╚═╝  ╚═══╝ ╚═════╝    ╚═╝   ╚═╝ ╚═════╝╚══════╝
// ------------------------------------------------
//
// This file is automatically generated.
// Please do not edit these files manually.
//
// ------------------------------------------------

#nullable restore

using Elastic.Clients.Elasticsearch.Fluent;
using Elastic.Clients.Elasticsearch.Serialization;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Elastic.Clients.Elasticsearch.MachineLearning;

[JsonConverter(typeof(DataframeAnalysisConverter))]
public sealed partial class DataframeAnalysis
{
	internal DataframeAnalysis(string variantName, object variant)
	{
		if (variantName is null)
			throw new ArgumentNullException(nameof(variantName));
		if (variant is null)
			throw new ArgumentNullException(nameof(variant));
		if (string.IsNullOrWhiteSpace(variantName))
			throw new ArgumentException("Variant name must not be empty or whitespace.");
		VariantName = variantName;
		Variant = variant;
	}

	internal object Variant { get; }
	internal string VariantName { get; }

	public static DataframeAnalysis Classification(Elastic.Clients.Elasticsearch.MachineLearning.DataframeAnalysisClassification dataframeAnalysisClassification) => new DataframeAnalysis("classification", dataframeAnalysisClassification);
	public static DataframeAnalysis OutlierDetection(Elastic.Clients.Elasticsearch.MachineLearning.DataframeAnalysisOutlierDetection dataframeAnalysisOutlierDetection) => new DataframeAnalysis("outlier_detection", dataframeAnalysisOutlierDetection);
	public static DataframeAnalysis Regression(Elastic.Clients.Elasticsearch.MachineLearning.DataframeAnalysisRegression dataframeAnalysisRegression) => new DataframeAnalysis("regression", dataframeAnalysisRegression);

	public bool TryGet<T>([NotNullWhen(true)] out T? result) where T : class
	{
		result = default;
		if (Variant is T variant)
		{
			result = variant;
			return true;
		}

		return false;
	}
}

internal sealed partial class DataframeAnalysisConverter : JsonConverter<DataframeAnalysis>
{
	public override DataframeAnalysis Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
	{
		if (reader.TokenType != JsonTokenType.StartObject)
		{
			throw new JsonException("Expected start token.");
		}

		object? variantValue = default;
		string? variantNameValue = default;
		while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
		{
			if (reader.TokenType != JsonTokenType.PropertyName)
			{
				throw new JsonException("Expected a property name token.");
			}

			if (reader.TokenType != JsonTokenType.PropertyName)
			{
				throw new JsonException("Expected a property name token representing the name of an Elasticsearch field.");
			}

			var propertyName = reader.GetString();
			reader.Read();
			if (propertyName == "classification")
			{
				variantValue = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.MachineLearning.DataframeAnalysisClassification?>(ref reader, options);
				variantNameValue = propertyName;
				continue;
			}

			if (propertyName == "outlier_detection")
			{
				variantValue = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.MachineLearning.DataframeAnalysisOutlierDetection?>(ref reader, options);
				variantNameValue = propertyName;
				continue;
			}

			if (propertyName == "regression")
			{
				variantValue = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.MachineLearning.DataframeAnalysisRegression?>(ref reader, options);
				variantNameValue = propertyName;
				continue;
			}

			throw new JsonException($"Unknown property name '{propertyName}' received while deserializing the 'DataframeAnalysis' from the response.");
		}

		var result = new DataframeAnalysis(variantNameValue, variantValue);
		return result;
	}

	public override void Write(Utf8JsonWriter writer, DataframeAnalysis value, JsonSerializerOptions options)
	{
		writer.WriteStartObject();
		if (value.VariantName is not null && value.Variant is not null)
		{
			writer.WritePropertyName(value.VariantName);
			switch (value.VariantName)
			{
				case "classification":
					JsonSerializer.Serialize<Elastic.Clients.Elasticsearch.MachineLearning.DataframeAnalysisClassification>(writer, (Elastic.Clients.Elasticsearch.MachineLearning.DataframeAnalysisClassification)value.Variant, options);
					break;
				case "outlier_detection":
					JsonSerializer.Serialize<Elastic.Clients.Elasticsearch.MachineLearning.DataframeAnalysisOutlierDetection>(writer, (Elastic.Clients.Elasticsearch.MachineLearning.DataframeAnalysisOutlierDetection)value.Variant, options);
					break;
				case "regression":
					JsonSerializer.Serialize<Elastic.Clients.Elasticsearch.MachineLearning.DataframeAnalysisRegression>(writer, (Elastic.Clients.Elasticsearch.MachineLearning.DataframeAnalysisRegression)value.Variant, options);
					break;
			}
		}

		writer.WriteEndObject();
	}
}

public sealed partial class DataframeAnalysisDescriptor<TDocument> : SerializableDescriptor<DataframeAnalysisDescriptor<TDocument>>
{
	internal DataframeAnalysisDescriptor(Action<DataframeAnalysisDescriptor<TDocument>> configure) => configure.Invoke(this);

	public DataframeAnalysisDescriptor() : base()
	{
	}

	private bool ContainsVariant { get; set; }
	private string ContainedVariantName { get; set; }
	private object Variant { get; set; }
	private Descriptor Descriptor { get; set; }

	private DataframeAnalysisDescriptor<TDocument> Set<T>(Action<T> descriptorAction, string variantName) where T : Descriptor
	{
		ContainedVariantName = variantName;
		ContainsVariant = true;
		var descriptor = (T)Activator.CreateInstance(typeof(T), true);
		descriptorAction?.Invoke(descriptor);
		Descriptor = descriptor;
		return Self;
	}

	private DataframeAnalysisDescriptor<TDocument> Set(object variant, string variantName)
	{
		Variant = variant;
		ContainedVariantName = variantName;
		ContainsVariant = true;
		return Self;
	}

	public DataframeAnalysisDescriptor<TDocument> Classification(Elastic.Clients.Elasticsearch.MachineLearning.DataframeAnalysisClassification dataframeAnalysisClassification) => Set(dataframeAnalysisClassification, "classification");
	public DataframeAnalysisDescriptor<TDocument> Classification(Action<Elastic.Clients.Elasticsearch.MachineLearning.DataframeAnalysisClassificationDescriptor<TDocument>> configure) => Set(configure, "classification");
	public DataframeAnalysisDescriptor<TDocument> OutlierDetection(Elastic.Clients.Elasticsearch.MachineLearning.DataframeAnalysisOutlierDetection dataframeAnalysisOutlierDetection) => Set(dataframeAnalysisOutlierDetection, "outlier_detection");
	public DataframeAnalysisDescriptor<TDocument> OutlierDetection(Action<Elastic.Clients.Elasticsearch.MachineLearning.DataframeAnalysisOutlierDetectionDescriptor> configure) => Set(configure, "outlier_detection");
	public DataframeAnalysisDescriptor<TDocument> Regression(Elastic.Clients.Elasticsearch.MachineLearning.DataframeAnalysisRegression dataframeAnalysisRegression) => Set(dataframeAnalysisRegression, "regression");
	public DataframeAnalysisDescriptor<TDocument> Regression(Action<Elastic.Clients.Elasticsearch.MachineLearning.DataframeAnalysisRegressionDescriptor<TDocument>> configure) => Set(configure, "regression");

	protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
	{
		writer.WriteStartObject();
		if (!string.IsNullOrEmpty(ContainedVariantName))
		{
			writer.WritePropertyName(ContainedVariantName);
			if (Variant is not null)
			{
				JsonSerializer.Serialize(writer, Variant, Variant.GetType(), options);
				writer.WriteEndObject();
				return;
			}

			JsonSerializer.Serialize(writer, Descriptor, Descriptor.GetType(), options);
		}

		writer.WriteEndObject();
	}
}

public sealed partial class DataframeAnalysisDescriptor : SerializableDescriptor<DataframeAnalysisDescriptor>
{
	internal DataframeAnalysisDescriptor(Action<DataframeAnalysisDescriptor> configure) => configure.Invoke(this);

	public DataframeAnalysisDescriptor() : base()
	{
	}

	private bool ContainsVariant { get; set; }
	private string ContainedVariantName { get; set; }
	private object Variant { get; set; }
	private Descriptor Descriptor { get; set; }

	private DataframeAnalysisDescriptor Set<T>(Action<T> descriptorAction, string variantName) where T : Descriptor
	{
		ContainedVariantName = variantName;
		ContainsVariant = true;
		var descriptor = (T)Activator.CreateInstance(typeof(T), true);
		descriptorAction?.Invoke(descriptor);
		Descriptor = descriptor;
		return Self;
	}

	private DataframeAnalysisDescriptor Set(object variant, string variantName)
	{
		Variant = variant;
		ContainedVariantName = variantName;
		ContainsVariant = true;
		return Self;
	}

	public DataframeAnalysisDescriptor Classification(Elastic.Clients.Elasticsearch.MachineLearning.DataframeAnalysisClassification dataframeAnalysisClassification) => Set(dataframeAnalysisClassification, "classification");
	public DataframeAnalysisDescriptor Classification<TDocument>(Action<Elastic.Clients.Elasticsearch.MachineLearning.DataframeAnalysisClassificationDescriptor> configure) => Set(configure, "classification");
	public DataframeAnalysisDescriptor OutlierDetection(Elastic.Clients.Elasticsearch.MachineLearning.DataframeAnalysisOutlierDetection dataframeAnalysisOutlierDetection) => Set(dataframeAnalysisOutlierDetection, "outlier_detection");
	public DataframeAnalysisDescriptor OutlierDetection(Action<Elastic.Clients.Elasticsearch.MachineLearning.DataframeAnalysisOutlierDetectionDescriptor> configure) => Set(configure, "outlier_detection");
	public DataframeAnalysisDescriptor Regression(Elastic.Clients.Elasticsearch.MachineLearning.DataframeAnalysisRegression dataframeAnalysisRegression) => Set(dataframeAnalysisRegression, "regression");
	public DataframeAnalysisDescriptor Regression<TDocument>(Action<Elastic.Clients.Elasticsearch.MachineLearning.DataframeAnalysisRegressionDescriptor> configure) => Set(configure, "regression");

	protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
	{
		writer.WriteStartObject();
		if (!string.IsNullOrEmpty(ContainedVariantName))
		{
			writer.WritePropertyName(ContainedVariantName);
			if (Variant is not null)
			{
				JsonSerializer.Serialize(writer, Variant, Variant.GetType(), options);
				writer.WriteEndObject();
				return;
			}

			JsonSerializer.Serialize(writer, Descriptor, Descriptor.GetType(), options);
		}

		writer.WriteEndObject();
	}
}